<?php
//------- logistic wxapp -------
namespace api\logi\controller;

use cmf\controller\RestUserBaseController;
use api\logi\service\PostService;
use api\logi\model\LogiPostModel;
use kuaidi\Kuaidi100;
use weixiao\Jssdk;
use wxpay\JsapiPay;
use think\Db;
class UserlogisController extends RestUserBaseController {

	/**
	 * 我的订单列表
	 */
	public function my() {
		$params = $this->request->get();
		$params['user_id'] = $this->getUserId();

		$postService = new PostService();
        $data        = $postService->postList($params);
        $data->appends($params);
        $list = $data->items();
        foreach ($list as $k => $v) {
        	$list[$k]['published_date'] = date('Y-m-d',$v['published_time']);
        	if ($v['pay_status'] == 2) {
        		$list[$k]['pay_name'] = '待支付';
        	} elseif ($v['pay_status'] == 1) {
        		$list[$k]['pay_name'] = '已支付';
        	} else {
        		$list[$k]['pay_name'] = '已关闭';
        	}
        }

		$this->success('请求成功!', $list);
	}

	/**
	 * 显示指定的资源
	 *
	 * @param  int $id
	 */
	public function read($id) {
		if (empty($id)) {
			$this->error('无效的订单id');
		}
		$params = $this->request->post();
		$params['id'] = $id;
		$params['user_id'] = $this->getUserId();

		$service = new PostService();
		$datas = $service->publishedLogi($params);
		// $post = new LogiPostModel();
		// $postCategories  = $datas->categories()->alias('a')->column('a.name', 'a.id');
  //       $datas['category'] = implode(',', $postCategories);
		$this->success('请求成功!', $datas);
	}

	/**
	 * 查看物流
	 *
	 * @param  int $id
	 */
	public function express() {
		$params = $this->request->param();
		$id = $params['id'];
		$status = $params['status'];
		if (empty($id)) {
			$this->error('无效的订单id');
		}
		$res = [];
		$kd = new Kuaidi100();
		$datas = Db::name('logiExpress')->where(['logi_id'=>$id,'status'=>['eq',$status] ])->field('express_num,code')->select();
		foreach ($datas as $k => $v) {
			$express = $kd->track(['com'=>$v['code'], 'num'=>$v['express_num'] ]);
			// var_dump($express);exit; 
			$res[$k]['num'] = $v['express_num'];
			if ($express['message'] == 'ok') {
				$res[$k]['data'] = $express['data'];
			}
		}

		$this->success('请求成功!', $res);
	}

	/**
	 * 支付
	 *
	 * @param  int $id
	 */
	public function toPay() {
		$param = $this->request->post();
		if (empty($param['id'])) {
			$this->error('无效的订单id');
		}
		$param['user_id'] = $this->getUserId();
		$where = ['id'=>$param['id']];
		$where['user_id'] = $param['user_id'];

		$service = new PostService();
		$data = $service->publishedLogi($where);
		if ($param['switch']=='true') {
			$param['mail_type'] = 1;
			$param['total'] = $data['more']['total'];
		} else {
			$param['mail_type'] = 2;
			$param['total'] = $data['more']['piece'];
		}
		$param['out_trade_no'] = date('YmdHis').'NO'.$param['id'];
		$post = new LogiPostModel();
		$post->where($where)->update(['price'=>$param['total'],'form_id'=>$param['form_id'],'out_trade_no'=>$param['out_trade_no'],'user_mail_type'=>$param['mail_type'],'mail_type'=>$param['mail_type']]);

		//-------支付 begin-------
		$wxpay = new JsapiPay();
		$openid = Db::name('third_party_user')->where(['user_id'=>$param['user_id'] ])->cache(true)->value('openid');
		$pay_data = [ 'body'=>'物流订单-NO.'.$param['id'],'out_trade_no'=>$param['out_trade_no'],'total_fee'=>$param['total']*100 ];
		$return = $wxpay->getParams($pay_data, $openid);
		//-------支付   end-------

		$this->success('请求成功!', json_decode($return));
	}

	//支付成功
	public function paySucceed(){
		$param = $this->request->post();
		if (empty($param['id'])) {
			$this->error('无效的订单id');
		}
		$post = new LogiPostModel();
		$post->where(['id'=>$param['id']])->update(['pay_status'=>1]);
		$this->success('请求成功!', [1]);
	}

	/**
	 * 删除指定资源
	 *
	 * @param  int $id
	 */
	public function close() {
		$param = $this->request->post();
		if (empty($param['id'])) {
			$this->error('无效的订单id');
		}
		$param['user_id'] = $this->getUserId();
		$post = new LogiPostModel();
		$post->where(['id'=>$param['id'],'user_id'=>$param['user_id']])->update(['post_status'=>0]);
		$this->success('删除成功！');
	}

	//变更快递状态
	public function switchTab(){
		$param = $this->request->post();
		if (empty($param['id'])) {
			$this->error('无效的订单id');
		}
		$post = new LogiPostModel();
		$data = $post->where(['id'=>$param['id']])->find()->toArray();
		$more = $data['more'];
		if ($param['switch'] == 'true' and !empty($param['switch'])) {  //真，将两个值相加
			$switch = 1;
			$price = $param['piece']+$param['price'];
		} else {  //假，将只存一个值，自提
			$switch = 2;
			$price = $param['piece'];
		}
		$more = json_encode($more);
		$post->where(['id'=>$param['id']])->update(['price'=>$price,'more'=>$more,'mail_type'=>$switch,'user_mail_type'=>$switch ]);
		$this->success('请求成功!', [1]);
	}
}